package com.gxnu.edu.cn.client.controller;

import com.gxnu.edu.cn.ttxxt.entity.FontSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class FontSettingClient {
    @Autowired
    private RestTemplate restTemplate;

    private String baseUrl = "http://localhost:8080";

    public CompletableFuture<FontSettings> addFontSetting(Long userId, String fontFamily, Integer fontSize,
                                                          String fontColor, String fontWeight, String fontStyle,
                                                          boolean isDefault) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Map<String, Object> request = new HashMap<>();
                request.put("userId", userId);
                request.put("fontFamily", fontFamily);
                request.put("fontSize", fontSize);
                request.put("fontColor", fontColor);
                request.put("fontWeight", fontWeight);
                request.put("fontStyle", fontStyle);
                request.put("isDefault", isDefault);

                ResponseEntity<FontSettings> response = restTemplate.postForEntity(
                        baseUrl + "/api/font-settings/",
                        request,
                        FontSettings.class
                );
                return response.getBody();
            } catch (Exception e) {
                e.printStackTrace(); // 打印详细异常信息
                throw new RuntimeException("添加字体设置失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<FontSettings> updateFontSetting(Long fontSettingId, Map<String, Object> request) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<FontSettings> response = restTemplate.exchange(
                        baseUrl + "/api/font-settings/" + fontSettingId,
                        HttpMethod.PUT,
                        new HttpEntity<>(request),
                        FontSettings.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("更新字体设置失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> deleteFontSetting(Long fontSettingId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<Map> response = restTemplate.exchange(
                        baseUrl + "/api/font-settings/" + fontSettingId,
                        HttpMethod.DELETE,
                        null,
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("删除字体设置失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Class<FontSettings>> getUserFontSettings(Long userId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity response = restTemplate.getForEntity(
                        baseUrl + "/api/font-settings/user/" + userId,
                        List.class
                );
                return FontSettings.class;
            } catch (Exception e) {
                throw new RuntimeException("获取用户字体设置失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<FontSettings> getDefaultFontSetting(Long userId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                ResponseEntity<FontSettings> response = restTemplate.getForEntity(
                        baseUrl + "/api/font-settings/user/" + userId + "/default",
                        FontSettings.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("获取默认字体设置失败: " + e.getMessage(), e);
            }
        });
    }

    public CompletableFuture<Map<String, Object>> setDefaultFontSetting(Long userId, Long fontSettingId) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Map<String, Long> request = Map.of("fontSettingId", fontSettingId);
                ResponseEntity<Map> response = restTemplate.exchange(
                        baseUrl + "/api/font-settings/user" + userId + "/default",
                        HttpMethod.PUT,
                        new HttpEntity<>(request),
                        Map.class
                );
                return response.getBody();
            } catch (Exception e) {
                throw new RuntimeException("设置默认字体设置失败: " + e.getMessage(), e);
            }
        });
    }
}